package com.lagou.server;

import com.lagou.common.ProviderLoader;
import com.lagou.common.RequestMetrics;
import com.lagou.common.ZkRegistryHandler;
import com.lagou.pojo.RpcRequest;
import com.lagou.service.IUserService;
import com.lagou.zookeeper.RPCRegistryHandler;
import com.lagou.zookeeper.config.ConfigKeeper;

import java.security.Provider;
import java.util.HashMap;
import java.util.UUID;

public class ClientBootstrap {
    public static final String providerName = "UserService#sayHello#";

    public static void main(String[] args) throws InterruptedException {
        rpc3Zookeeper(args);
    }
/*

    private void rpc1() throws InterruptedException {
        RpcConsumer consumer = new RpcConsumer();
        IUserService service = (IUserService) consumer.createProxy(IUserService.class, providerName);
        for(;;){
            Thread.sleep(1000);
            System.out.println(service.sayHello("are u ok ?"));
        }
    }

    private static void rpc2() throws InterruptedException {
        RpcConsumer consumer = new RpcConsumer();

        IUserService service = (IUserService) consumer.createProxy2(IUserService.class);
        while(true){
            System.out.println("开始组装参数=======>>>");
            RpcRequest msg = new RpcRequest();
            msg.setRequestId(UUID.randomUUID().toString());
            msg.setClassName("Name");
            msg.setMethodName("bark");
            msg.setParameters(new Object[]{"tom",666});
            msg.setParameterTypes(new Class[]{int.class, Void.class, Enum.class});
            System.out.println("客户端入参：" + msg.toString());
            String result = service.invoke(msg);
            System.out.println("客户端接收到出参：" + result);
            Thread.sleep(3000);
        }
    }
*/

    /**
     * part3 启动类
     * <pre>
     *     1. 设置注册服务类
     *     2. 注册zk监听服务
     *     3.
     * </pre>
     * @param args
     * @throws InterruptedException
     */
    private static void rpc3Zookeeper(String []args) throws InterruptedException {
        /* 1. 设置注册服务类参数 */
        HashMap<String, Object> instanceCacheMap = new HashMap<>();
        instanceCacheMap.put(IUserService.class.getName(), IUserService.class);
        final ConfigKeeper instance = ConfigKeeper.getInstance();

        instance.setZkAddr(args[0]);
        // 启动一个定时器，每5秒自动上报统计数据到注册中心
        instance.setInterval(5);
        // false client
        instance.setProviderSide(false);

        /* 2. 注册zk监听服务*/
        RPCRegistryHandler zkRegistryHandler = new ZkRegistryHandler(instance.getZkAddr());
        System.out.println("======客户端 Zookeeper session establish");

        /* 3. 注册rpc调用服务*/
        final RpcConsumer consumer = new RpcConsumer(zkRegistryHandler, ProviderLoader.getInstanceCacheMap());
        IUserService userService = consumer.createProxyEnhance(IUserService.class);
        while(true){
            Thread.sleep(5000);
            try {
                userService.sayHello("are u ready?");
                System.out.println("服务已响应！");
            }catch (Exception e){
                // 服务上线下线重新指定代理类
                userService = consumer.createProxyEnhance(IUserService.class);
            }
        }
    }
}
